package com.wenwei.view;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.jakewharton.rxbinding.widget.RxTextView;
import com.orhanobut.logger.Logger;
import com.wenwei.adapter.PopupAdapter;
import com.wenwei.bean.UserParkBean;
import com.wenwei.parking.R;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func2;

/**
 * Created by Administrator on 2017/07/12 0012.
 */
public class SubscriberPw extends PopupWindow {

    Activity mActivity;
    PopupWindow popupWindow;


    public CallBack mCallBack;


    public interface CallBack{
        void onClose(int position,int id);
    }


    private RecyclerView recyclerView;
    private PopupAdapter mAdapter;

    public SubscriberPw(Activity activity, View parentView, final CallBack callBack, ArrayList<UserParkBean> list) {
        this.mCallBack = callBack;
        mActivity = activity;
        View contentView = LayoutInflater.from(activity).inflate(
                R.layout.popup, null);

        recyclerView = (RecyclerView) contentView.findViewById(R.id.recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        mAdapter = new PopupAdapter(list, new PopupAdapter.CallBack() {
            @Override
            public void onItemClick(int position,int parkId) {
                popupWindow.dismiss();
                mCallBack.onClose(position,parkId);
            }
        });
        recyclerView.setAdapter(mAdapter);

        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
//        ColorDrawable cd = new ColorDrawable(0x000000);
//        popupWindow.setBackgroundDrawable(cd);

//        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
//        lp.alpha = 0.4f;
//        activity.getWindow().setAttributes(lp);

//        backgroundAlpha(0.4f);
        popupWindow.showAsDropDown(parentView);
//        popupWindow.setOnDismissListener(new OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                mCallBack.onClose();
//                backgroundAlpha(1f);
//            }
//        });
    }


    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mActivity.getWindow().setAttributes(lp);
    }


    /**
     * 改变背景颜色
     */
    private void darkenBackground(Float bgcolor) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgcolor;
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mActivity.getWindow().setAttributes(lp);
    }



}
